package com.ruoze.bdp.service.impl;

import com.ruoze.bdp.dao.IClusterRepository;
import com.ruoze.bdp.entity.ClusterEntity;
import com.ruoze.bdp.entity.ClusterVo;
import com.ruoze.bdp.entity.cluster.ClusterRequest;
import com.ruoze.bdp.service.ClusterService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

@Service
public class ClusterServiceImpl implements ClusterService {
    @Autowired
    private IClusterRepository repository;

    @Override
    public Page<ClusterEntity> queryPage(ClusterRequest request) {
        String name = request.getName();

        Specification<ClusterEntity> queryFilter = (root, query, criteriaBuilder) -> {
            List<Predicate> predicateList = new ArrayList<>();  //所有的断言
            if (StringUtils.isNotEmpty(name)) {
                predicateList.add(criteriaBuilder.like(root.get("name").as(String.class),"%" + name+ "%"));
            }
            predicateList.add(criteriaBuilder.equal(root.get("status"),0)); // 查询status = 0的数据
            return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
        };

        Integer pageNo = request.getPageNum() <= 0 ? 0 : request.getPageNum() - 1;   // 当前页码：前端从1开始，jpa从0开始，做个转换
        Integer pageSize =request.getPageSize() <= 10 ? 10 : request.getPageSize();  //每页数量

        //分页信息
        Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.by(Sort.Direction.ASC, "id")); //页码：前端从1开始，jpa从0开始，做个转换
        //查询
        Page<ClusterEntity> page = repository.findAll(queryFilter, pageable);

        return page;
    }

    @Override
    public void createNewCluster(ClusterVo newCluster) {
        ClusterEntity clusterEntity = clusterVo2Entity(newCluster);
        repository.save(clusterEntity);
    }

    @Override
    public void replaceNewCluster(ClusterVo newCluster, long id) {
        ClusterEntity newClusterEntity = clusterVo2Entity(newCluster);
        repository.findById(id).map(old -> {
            old.setName(newCluster.getName());
            old.setNote(newCluster.getNote());
            old.setNameservice(newCluster.getNameservice());
            return repository.save(old);
        }).orElseGet(() -> repository.save(newClusterEntity));
    }

    @Override
    public void logicalDelCluster(long id) {
        repository.logicalDeleteById(id);
    }

    @Override
    public void logicalBatchDelCluster(List<Long> idList) {
        repository.logicalDeleteBatch(idList);
    }

    private ClusterEntity clusterVo2Entity(ClusterVo vo) {
        ClusterEntity clusterEntity = new ClusterEntity();
        clusterEntity.setName(vo.getName().trim());
        clusterEntity.setNote(vo.getNote().trim());
        clusterEntity.setNameservice(vo.getNameservice().trim());
        clusterEntity.setCreatedUserId(vo.getUserId());
        clusterEntity.setCreatedUserName(vo.getUserName());
        return clusterEntity;
    }

    @Override
    public List<ClusterEntity> queryAll() {

        Specification<ClusterEntity> queryFilter = (root, query, criteriaBuilder) -> {
            List<Predicate> predicateList = new ArrayList<>();  //所有的断言
            predicateList.add(criteriaBuilder.equal(root.get("status"),0)); // 查询status = 0的数据
            return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
        };
        return repository.findAll(queryFilter);
    }
}
